package com.irdstudio.efp.esb.api.common;


import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;
import com.irdstudio.basic.framework.core.base.FrameworkService;
import com.irdstudio.efp.esb.api.service.sed.SedCommonService;
import com.irdstudio.efp.esb.common.server.req.EsbReqServiceBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ModelAttribute;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.ParameterizedType;

/**
 * @author gcb
 */
public class EsbFrontCommonController<T,R> implements FrameworkService {


    @Autowired
    public SedCommonService<R> sedCommonService;

    /**
     * 获取ESB请求对象，校验 ESB请求系统头和请求应用头对象
     * @param request 请求对象集合
     * @return
     */
    @ModelAttribute("esbReqServiceBean")
    public EsbReqServiceBean getReqEsbHeadBean(HttpServletRequest request) {
        EsbReqServiceBean esbReqServiceBean = (EsbReqServiceBean) request.getAttribute("esbReqServiceBean");
        return esbReqServiceBean;
    }

    /**
     * 获取ESB请求对象，校验 ESB请求体对象
     * @param request 请求对象集合
     * @return
     */
    @ModelAttribute("getReqEsbBodyBean")
    public T getReqEsbBodyBean(HttpServletRequest request) {
        Class<T> actualTypeArgument = (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
        String esbReqBody = (String) request.getAttribute("esbReqBody");
        return JSONObject.parseObject(esbReqBody, actualTypeArgument, Feature.InternFieldNames);
    }



}
